const { ccclass, property } = cc._decorator;
import WinNodeBase from '../WinNodeBase';
import loadLobby from '../Load/loadLobby';
import taskItem from './taskListControl';
import MyEnums from '../MyEnums';
import skillCounter from '../skillCounter';
import GradeControl from './GradeControl';

enum targetType {
    skill = 0,//击杀怪兽数统计
    star = 1.//三星闯关数统计
}

@ccclass
export default class taskControl extends WinNodeBase {
    @property(cc.Node)
    content: cc.Node = null;
    @property(cc.Node)
    gradeNode: cc.Node = null;

    _taskItemPfb: cc.Prefab = null;
    _taskIcons: cc.SpriteAtlas = null;
    _taskJson: any = null;
    isInit: boolean = false;

    init(): boolean {
        if (!this.isInit) {
            //获取常驻节点所绑定的脚本 
            let control = cc.director.getScene().getChildByName('loadLobby').getComponent(loadLobby);
            this._taskIcons = control._awardIcons;
            this._taskItemPfb = control.TaskPfb;
            this._taskJson = control.TaskJson;
            this.loadItem();
            this.isInit = true;
        }
        return this.updateTask();
    }

    updateTask(): boolean {
        if (this.content.children.length == 0)
            return this.init();
        else {
            let _skillCounter: skillCounter = new skillCounter();
            let count = _skillCounter.getCounter();
            let finished = 0;

            let start3 = 0;
            let heroh: number[] = gameHelper.getInstance().levelParam;
            heroh.forEach(element => {
                if (element == 3)
                    start3++;
            });

            this.content.children.forEach(element => {
                let control = element.getComponent(taskItem);
                let _describeTo = 0;
                if (control._targetType == targetType.skill) {//击杀数
                    switch (control._timeType) {
                        case MyEnums.counterType.day:
                            _describeTo = count.day;
                            break;
                        case MyEnums.counterType.week:
                            _describeTo = count.week + count.day;
                            break;
                        case MyEnums.counterType.month:
                            _describeTo = count.month + count.day;
                            break;
                    }
                }
                else if (control._targetType == targetType.star) {//三星通关数
                    _describeTo = start3;
                }
                else {
                    //待续
                }
                if (control.showdescribe(_describeTo))
                    finished++;
            });
            if (finished > 0)
                return true;
            else
                return false;
        }
    }

    getIcon(type: number): cc.SpriteFrame {
        switch (type) {
            case MyEnums.AwardType.exp:
                return this._taskIcons.getSpriteFrame("exp");
            case MyEnums.AwardType.ftp:
                return this._taskIcons.getSpriteFrame("ftg");
            case MyEnums.AwardType.money:
                return this._taskIcons.getSpriteFrame("money");
            case MyEnums.AwardType.skill:
                return this._taskIcons.getSpriteFrame("skill");
        }
    }

    loadItem() {
        let _skillCounter: skillCounter = new skillCounter();

        let self = this;
        this._taskJson.task.forEach(element => {
            if (!_skillCounter.checkAwared(element.Id, element.TimeType)) {
                let node = cc.instantiate(self._taskItemPfb);
                let control = node.getComponent(taskItem);
                control.titelLabel.string = element.Titel;
                control.degreeLabel.string = element.Degree;
                control.awardCountLabel.string = element.Count;
                control.awardType.spriteFrame = self.getIcon(element.AwardType);
                control.finishedSprite = this._taskIcons.getSpriteFrame("finished");
                control._awardType = element.AwardType;
                control._awardCount = element.Count;
                control._describe = element.Describe;
                control._targetType = element.Target;
                control._timeType = element.TimeType;
                control._id = element.Id;
                control.getAwardBt.clickEvents.push(this.setBtClick({ target: node }));
                self.content.addChild(node);
            }
        });
    }

    private setBtClick(date: any): cc.Component.EventHandler {
        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node;
        clickEventHandler.component = "taskControl";
        clickEventHandler.handler = "getAward";
        clickEventHandler.customEventData = date;
        return clickEventHandler;
    }

    public getAward(event, customEventData) {
        let control: taskItem = customEventData.target.getComponent(taskItem);
        if (control._isFinished) {
            let param = gameHelper.getInstance().gradeParam;
            let Details = this.gradeNode.getComponent(GradeControl);

            switch (control._awardType) {
                case MyEnums.AwardType.exp:
                    param.exp += control._awardCount;
                    gameHelper.getInstance().gradeParam = param;
                    Details.showExp(param.exp);
                    break;
                case MyEnums.AwardType.ftp:
                    param.ftg += control._awardCount;
                    gameHelper.getInstance().gradeParam = param;
                    Details.showExp(param.ftg);
                    break;
                case MyEnums.AwardType.money:
                    param.money += control._awardCount;
                    gameHelper.getInstance().gradeParam = param;
                    Details.showExp(param.money);
                    break;
                case MyEnums.AwardType.skill:
                    //待续
                    break;
            }
            customEventData.target.removeFromParent();
            customEventData.target.active = false;
            let _skillCounter: skillCounter = new skillCounter();
            _skillCounter.setAwared(control._id, control._timeType);
        }
    }
}
